package com.ideal.manage.esms.controller.monitor;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ideal.manage.esms.bean.DTO.PageDto;
import com.ideal.manage.esms.service.monitor.MonitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 系统监控
 * Created By Dai On 2018-03-26
 */
@Controller
@RequestMapping("monitor")
public class MonitorController {
    private Logger logger = LoggerFactory.getLogger(MonitorController.class);

    @Resource
    private MonitorService monitorService;
    /**
     * 系统监控页面
     */
    @RequestMapping("monitor")
    public void monitorPage(Model model, Long id){
        model.addAttribute("id", id);
        List<JSONObject> list = monitorService.getMonitorList();
        model.addAttribute("list", list);
    }

    /**
     * 系统监控内嵌页面统一转发
     */
    @RequestMapping("monitor_web")
    public void monitorWebPage(HttpServletResponse servletResponse, Long id){
        int type = monitorService.getType(id);
        try {
            // 根据类型转发
            switch (type) {
                case 1:
                    servletResponse.sendRedirect("monitor_web_for_block?id="+id);
                    break;
                case 2:
                    servletResponse.sendRedirect("monitor_web_for_PCS?id="+id);
                    break;
                case 3:
                    servletResponse.sendRedirect("monitor_web_for_package?id="+id);
                    break;
                case 4:
                    servletResponse.sendRedirect("monitor_web_for_group?id="+id);
                    break;
                case 5:
                    servletResponse.sendRedirect("monitor_web_for_one?id="+id);
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    ----------------------------------------- 转 发 -----------------------------------------

    /**
     * 系统监控内嵌页面 - 楼宇级
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_block")
    public void monitorWebForBlock(Model model, Long id){
        // 1. 标题栏
        model.addAttribute("id", id);
        String name = monitorService.getName(id);
        model.addAttribute("name", name);
        // 2. 实时监控 - 实时概况
        JSONObject monitorData = monitorService.getMonitorDataForBlock(id);
        model.addAttribute("monitorData", monitorData);
        // 3. 实时监控 - 电池包总览
        List packageInfo = monitorService.packageInfoData(id);
        model.addAttribute("packageInfo", packageInfo);
        // 4. 台帐信息 - 项目信息
        JSONObject projectData = monitorService.getProjectDataForBlock(id);
        model.addAttribute("projectData", projectData);
        // 5. 台帐信息 - 系统信息
        JSONObject systemData = monitorService.getSystemDataForBlock(id);
        model.addAttribute("systemData", systemData);
    }

    /**
     * 系统监控内嵌页面 - PCS级
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_PCS")
    public void monitorWebForPCS(Model model, Long id){
        model.addAttribute("id", id);
        String name = monitorService.getName(id);
        model.addAttribute("name", name);
        String parentName = monitorService.getParentName(id);
        model.addAttribute("parentName", parentName);
    }

    /**
     * 系统监控内嵌页面 - 电池包级
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_package")
    public void monitorWebForPackage(Model model, Long id){
        model.addAttribute("id", id);
        String name = monitorService.getName(id);
        model.addAttribute("name", name);
        String parentName = monitorService.getParentName(id);
        model.addAttribute("parentName", parentName);
    }

    /**
     * 系统监控内嵌页面 - 电池包级
     * @param model
     * @param id
     */
    @RequestMapping("monitor_web_for_group")
    public void monitorWebForGroup(Model model, Long id){
        model.addAttribute("id", id);
        String name = monitorService.getName(id);
        model.addAttribute("name", name);
        String parentName = monitorService.getParentName(id);
        model.addAttribute("parentName", parentName);
        String grandparentName = monitorService.getGrandparentName(id);
        model.addAttribute("grandparentName", grandparentName);
    }

    /**
     * 系统监控内嵌页面 - 电池级
     * @param model
     */
    @RequestMapping("monitor_web_for_one")
    public void monitorWebForOne(Model model, Long id){
        model.addAttribute("id", id);
        String name = monitorService.getName(id);
        model.addAttribute("name", name);
        String parentName = monitorService.getParentName(id);
        model.addAttribute("parentName", parentName);
        String grandparentName = monitorService.getGrandparentName(id);
        model.addAttribute("grandparentName", grandparentName);
        String grandparentParentName = monitorService.getGrandparentParentName(id);
        model.addAttribute("grandparentParentName", grandparentParentName);
    }

//    ----------------------------------------- 楼 宇 级 -----------------------------------------
    /**
     * 根据id刷新 可储蓄电量
     * @param id
     * @return
     */
    @RequestMapping("refresh_could_saving_electricity")
    @ResponseBody
    public String refreshCouldSavingElectricity(Long id){
        return monitorService.refreshCouldSavingElectricity(id);
    }

    /**
     * 根据id刷新 总容量
     * @param id
     * @return
     */
    @RequestMapping("refresh_total_electricity")
    @ResponseBody
    public String refreshTotalElectricity(Long id){
        return monitorService.refreshTotalElectricity(id);
    }

    /**
     * 系统监控页面 - 楼宇级
     * 实时监控 - 故障信息
     * @param id
     * @return
     */
    @RequestMapping("error_data")
    @ResponseBody
    public JSONArray errorData(Long id){
        JSONArray list = monitorService.errorData(id);
        return list;
    }

    /**
     * 实时监控 今日充放电量、电费曲线
     * @param id
     * @return
     */
    @RequestMapping("today_electricity")
    @ResponseBody
    public Map todayElectricity(Long id){
        return monitorService.todayElectricity(id);
    }

    /**
     * 实时监控 电池包总览 柱状图
     * @param id
     * @return
     */
    @RequestMapping("package_info")
    @ResponseBody
    public Map packageInfo(Long id){
        return monitorService.packageInfo(id);
    }

    /**
     * 系统监控内嵌页面 - 楼宇级
     * 台帐信息 - 分时电价表
     * @return
     */
    @RequestMapping("tou_electricity_pricing")
    @ResponseBody
    public JSONArray touElectricityPricing(){
        JSONArray list = new JSONArray();
        JSONObject jsonObject ;
        for (int i = 1; i < 4; i++){
            jsonObject= new JSONObject();
            jsonObject.put("time_name", i + "#时段名称");
            jsonObject.put("time",i + "#时段字符串");
            jsonObject.put("price", i + "#价格");
            list.add(jsonObject);
        }
        return list;
    }

//    ----------------------------------------- 电 池 包 -----------------------------------------

    /**
     * 获取电池包中电压最高与最低的5个单体
     * @return
     */
    @RequestMapping("get_voltage_data")
    @ResponseBody
    public JSONArray getVoltageData(Long id){
        JSONArray jsonArray = monitorService.getVoltageData(id);
        return jsonArray;
    }

    /**
     * 获取电池包中温度最高与最低的5个单体
     * @return
     */
    @RequestMapping("get_temperature_data")
    @ResponseBody
    public JSONArray getTemperatureData(Long id){
        JSONArray jsonArray = monitorService.getTemperatureData(id);
        return jsonArray;
    }


}
